﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PMPUtils.Form;
using PMPUtils.Goals;

namespace PMP.Goals
{
    public partial class GoalManagement_ChangeExpectedDueDate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FormUtil.Authenticate(Session, Response);

            if (!IsPostBack)
            {
                txtGoal.Text = Request.Params["PARENT_ID"];
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            String goalID = txtGoal.Text.Trim();
            String oldDueDate = GoalsOperator.ReadGoalAttributes(goalID, "est_due_date");
            String newDueDate = calExpected.SelectedDate.ToString("yyyy-MM-dd");

            if (newDueDate.Equals("0001-01-01"))
            {
                Messenger.Show(Response, "Please select the new due date.");
                return;
            }

            try
            {
                GoalsOperator.ChangeExpectedDueDate(goalID, newDueDate);
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error: " + ex.Message);
                return;
            }

            //Send update
            String goalTitle = GoalsOperator.ReadGoalDesc(txtGoal.Text.Trim());
            String subject = "Goal <" + goalTitle + "> expected due date <" + oldDueDate + "> is updated to <" + newDueDate + ">.";

            String userName = FormUtil.GetSessionUserName(Session);
            GoalsOperator.SendUpdateMessage(goalID, subject, userName);

            Messenger.Show(Response, "Update Successfully.");
            Close();
        }

        private void Close()
        {
            PopupForm changeExpectedDueDate = new PopupForm(Response);
            changeExpectedDueDate.Close();
        }
    }
}
